package com.example.mybaseapp.ui.contentproviderdemo

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.net.Uri
import com.example.mybaseapp.util.dbutil.DatabaseHelper

class MyContentProvider : ContentProvider() {

    private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
        addURI(AUTHORITY, "items", ITEMS)
        addURI(AUTHORITY, "items/#", ITEM_ID)
    }

    private lateinit var dbHelper: DatabaseHelper

    override fun onCreate(): Boolean {
        dbHelper = DatabaseHelper(context!!)
        return true
    }

    override fun query(
        uri: Uri,
        projection: Array<String>?,
        selection: String?,
        selectionArgs: Array<String>?,
        sortOrder: String?
    ): Cursor? {
        return when (uriMatcher.match(uri)) {
            ITEMS -> dbHelper.readableDatabase.query("items", projection, selection, selectionArgs, null, null, sortOrder)
            ITEM_ID -> dbHelper.readableDatabase.query(
                "items",
                projection,
                "_id=?",
                arrayOf(uri.lastPathSegment),
                null,
                null,
                sortOrder
            )
            else -> throw IllegalArgumentException("Unknown URI: $uri")
        }
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? {
        val id = when (uriMatcher.match(uri)) {
            ITEMS -> dbHelper.writableDatabase.insert("items", null, values)
            else -> throw IllegalArgumentException("Unknown URI: $uri")
        }
        context?.contentResolver?.notifyChange(uri, null)
        return Uri.parse("content://$AUTHORITY/items/$id")
    }

    override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
        val count = when (uriMatcher.match(uri)) {
            ITEM_ID -> dbHelper.writableDatabase.update("items", values, "_id=?", arrayOf(uri.lastPathSegment))
            else -> throw IllegalArgumentException("Unknown URI: $uri")
        }
        context?.contentResolver?.notifyChange(uri, null)
        return count
    }

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
        val count = when (uriMatcher.match(uri)) {
            ITEM_ID -> dbHelper.writableDatabase.delete("items", "_id=?", arrayOf(uri.lastPathSegment))
            else -> throw IllegalArgumentException("Unknown URI: $uri")
        }
        context?.contentResolver?.notifyChange(uri, null)
        return count
    }

    override fun getType(uri: Uri): String? {
        return when (uriMatcher.match(uri)) {
            ITEMS -> "vnd.android.cursor.dir/vnd.$AUTHORITY.items"
            ITEM_ID -> "vnd.android.cursor.item/vnd.$AUTHORITY.items"
            else -> throw IllegalArgumentException("Unknown URI: $uri")
        }
    }

    companion object {
        private const val AUTHORITY = "com.example.mycontentprovider"
        private const val ITEMS = 1
        private const val ITEM_ID = 2
    }
}